﻿var manager = {
    //课程名称
    class_name: '',
    //本课程已完成数
    class_done : '',
    //一大班
    groupone : [],
    //二大班
    grouptwo : [],
	//本次课所有学生
	attendents : [],

    init : function(){
        //获取课程名称
        class_name = $.getUrlVar('class');
        class_done = $.getUrlVar('done');

        var lessons = [], fragment, lessonid;

        /*************************/
        /**面包屑加载/
        /*************************/
        if(class_name != ''){
            fragment = '<li><a href="index.html">所有课程</a><span class="divider">/</span></li><li class="active">'+ class_name +'<div class="progress progress-success progress-striped" data-original-title="已完成'+ class_done +'%课程"><div class="bar" style="width:'+ class_done +'%;"></div></div></li>';
            $(fragment).appendTo($('#article #title ul'));
        }
        //为任务进度条添加提示信息功能
        $('.progress').tooltip('hide');

        /*************************/
        /**加载课次列表LoadLessonList.action/
        /*************************/
        var lessonnavDIV = $('#wrap>.leftSchema>ul');
        $.getJSON("LoadLessonList.action", function(data){
            
            $.each(data.lessons, function(i, item){
               fragment = '<li class="nav-line" id="'+ item.id +'"><a href="#"><span class="col1">'+ item.lessonNum +'</span><span class="col2">'+ item.lessonTitle +'</span></a></li>';

               $(fragment).appendTo(lessonnavDIV);
            });
            //为所有课次绑定点击事件
            $('#wrap>.leftSchema>ul>li').live("click", function(){
                //给“点击某一课次”绑定事件LoadLessonNotice.action 和 LoadAttend.action
                lessonid = $(this).attr('id');
                $.getJSON("LoadLessonNotice.action",{id: lessonid}, function(data){
                    //载入公告内容，然后展开
                    $('#words .accordion-inner').html(data.notice);
                    $('#words').collapse('show');
                });

                $.getJSON("LoadAttend.action",{id: lessonid}, function(data){
                    //载入本次课学生名单
                    var loc = $('#thisRecord table tbody');
                    //保存所有学生信息到attendents数组中
                    manager.attendents = data.attends;
					console.log('att:'+manager.attendents);
		manager.divideGroup(manager.attendents);
                    $.each(manager.attendents, function(i, attend){
                        fragment = '<tr><td>'+ attend.name +'</td><td>'+ attend.num +'</td><td>'+ attend.classz+'</td><td>'+ attend.sex +'</td><td>'+ attend.thisrecord +'</td><td><a data-toggle="modal" href="#modifyModal">修改</a></td><td><a data-toggle="modal" href="#detailModal">查看</a></td></tr>';
                        $(fragment).appendTo(loc);
                    });
                    
                });
                var lis = $('#wrap>.leftSchema>ul>li');
                lis.removeClass('active');
                $(this).addClass("active");
            });
        });
    
        /*************************/
        /**加载本课程学生总考勤记录LoadAttendRecord.action/
        /*************************/        
        $.getJSON("LoadAttendRecord.action", function(data){
            var loc = $('#totalRecord table tbody');
            $.each(data.attends, function(i, attend){
                fragment = '<tr><td>'+ (i+1) +'</td><td>'+ attend.name +'</td><td>'+ attend.num +'</td><td>'+ attend.classz +'</td><td>'+ attend.sex +'</td><td>'+ attend.totalWrong+'</td><td>'+ attend.leave +'</td><td>'+ attend.late +'</td><td>'+ attend.wrong +'</td><td><a data-toggle="modal" href="#detailModal">详细</a></td></tr>';

                $(fragment).appendTo(loc);
            });
        });

        /*************************/
        /**加载aside部分的内容/
        /*************************/
        var cla_Cplus = {"title":"C++程序语言设计","info":"60学时,2.5学分,必修","NO":"0101011111","school":"软件与微电子学院","attendent":"软件工程专业09级本科生"};
        var cla_interface = {"title":"人机界面","info":"50学时,2学分,必修","NO":"0101011112","school":"软件与微电子学院","attendent":"软件工程专业09级本科生"};
        var cla_datastructure = {"title":"数据结构与算法深入研究","info":"64学时,3.5学分,必修","NO":"0101011113","school":"软件与微电子学院","attendent":"软件工程专业10级研究生"};

        var asideDiv = $('#slidecontent');
        var cla_show, fragment;
        switch(class_name){
            case 'C++程序设计': 
                cla_show = cla_Cplus;
                break;
            case '人机界面':
                cla_show = cla_interface;
                break;
            case '数据结构与算法深入研究':
                cla_show = cla_datastructure;
                break;
            default:
                alert('没有这门课程相关信息，出错了~');
                break;
        };
        if(cla_show != undefined){
            fragment = '<section><p class="title">'+ cla_show.title +'</p><p class="info1">'+cla_show.info+'</p><p class="info2">课程编号：'+ cla_show.NO+'</p><p class="info3">开课对象：'+ cla_show.school +'</p><p class="info4">'+ cla_show.attendent +'</p>';
        };

        $(fragment).appendTo(asideDiv);

        /*************************/
        /**编辑课程公告内容/
        /*************************/
        $('#editor .modal-footer .btn-primary').click(function(){
            var content = $('#editor>.modal-body>textarea').val();
            manager.editNotice(content);
        });
		
        /*************************/
        /**本次课考勤系统中的数据加载/
        /*************************/ 
		
        $('#thisCallRoll form #chooseClass').change(function(){
            var selectDom = $('#thisCallRoll form select');
            var selectedValue = $("select option:selected").attr('value');
			$('#showtable table tbody').html('');
            switch (selectedValue){
                case '0'://请选择
                    $('#showtable').css('display','none');
                    break;
                case '1'://一大班
                    $('#showtable').css('display','block');
					console.log('manager.groupone:'+manager.groupone.length);
                    manager.showTwoStudents(manager.getStudentsNoRecord(manager.groupone));
                    break;
                case '2'://二大班
                    $('#showtable').css('display','block');
					console.log('manager.grouptwo:'+manager.grouptwo.length);
                    manager.showTwoStudents(manager.getStudentsNoRecord(manager.grouptwo));
                    break;
                default:
                    break;
            }
        });
    },
    
    //编辑课程公告
    editNotice : function(str){
        //发送到数据库保存
        $.post('modifyNotice.action',{con: str}, function(data){
            $('#editor').modal('hide');
            $('#words .accordion-inner').html(str);
            $('#words').collapse('show');
        },'html');
        //~~测试使用~~~
        $('#editor').modal('hide');
        $('#words .accordion-inner').html(str);
        $('#words').collapse('show');
    },

    //按大班分组
    divideGroup : function(arr){
        var ONE = ["14010901","RJ010902","RJ010903","RJ010904","RJ010905"],
            TWO = ['RJ010906','RJ010907','RJ010908','RJ010909','RJ020901','RJ020902'];

        $.each(arr, function(i, student){
            //分大班
            if($.inArray(student.classz, ONE) != -1){
                (manager.groupone).push(student);
            }else if ($.inArray(student.classz, TWO) != -1){
                (manager.grouptwo).push(student);
            }
        });

    },

    //获取本次课尚未考勤记录的学生
    getStudentsNoRecord: function(group){
        var students = [];
        $.each(group, function(i, student){
            //获取未
            if(student.thisrecord == ""){
                students.push(student);
            }
        });

        return students;
    },

    //显示两行，即两个学生信息
    showTwoStudents: function(arr){
        var fragment;
console.log('length:'+arr.length);
        if(arr.length > 1){
            var first = arr.shift();
            var second = arr.shift();
            fragment = '<tr class="success"><td>'+ first.name+'</td><td>'+ first.num +'</td><td>'+ first.classz +'</td><td>'+ first.sex +'</td><td>'+ first.thisrecord+'</td><td><a dtoggle="modal" href="#detailModal">查看</a></td></tr><tr><td>'+ second.name+'</td><td>'+ second.num +'</td><td>'+ second.classz +'</td><td>'+ second.sex +'</td><td>'+ second.thisrecord+'</td><td><a dtoggle="modal" href="#detailModal">查看</a></td></tr>';

        }else if(arr.length == 1){
            var first = arr.shift();
            fragment = '<tr><td>'+ first.name+'</td><td>'+ first.num +'</td><td>'+ first.classz +'</td><td>'+ first.sex +'</td><td>'+ first.thisrecord+'</td><td><a data-toggle="modhref="#detailModal">查看</a></td></tr><tr style="display:none"><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>';
        }else{
			fragment = '';
		}
     

        $(fragment).appendTo($('#showtable table tbody'));
        
        return arr;
    }



}

$(document).ready(function(){
    base.init();
    manager.init();
});